<%@ page import="db.DBConnection" %>
<%@ page import="db.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="db.Account" %>
<%
    int user_id = -1;
    String secret1 = "";
    Cookie[] cookies = request.getCookies();

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("user_id")) {
            user_id = Integer.parseInt(cookie.getValue());
        } else if (cookie.getName().equals("secret")) {
            secret1 = cookie.getValue();
        }
    }
    DBConnection dbConnection = new DBConnection();
    if (!dbConnection.isCookieOk(user_id, secret1)) {
        String redirectURL = "index.jsp";
        response.sendRedirect(redirectURL);
    }
    int num = dbConnection.doGetNumberOfUnreadMessages(user_id);
%>

<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 07.05.12
  Time: 19:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Accounts</title>
    <link rel="stylesheet" type="text/css" href="style"/>
</head>
<body>
<div class="logout" style="text-align:right;"><a href="logout.jsp">  Logout</a></div>
<div class="header"><img src="logo.jpg"></div>
<div class="layout">
    <div class="sidebar">
        <ul class="cssmenu">
            <li><a href="personal_info.jsp">Personal info</a></li>
            <li><b><a href="accounts.jsp">Accounts</a></b></li>
            <li><a href="operations.jsp">Operations</a></li>
            <li><a href="messages.jsp">Messages<% if (num != 0) {%> <b> (<%=num%>)</b> <%}%></a></li>
        </ul>
    </div>
    <div class="content">
        <%
            ArrayList<Account> accounts = dbConnection.getAccounts(user_id,user_id, DBConnection.OwnerType.CLIENT);
            if (accounts.size() == 0) {
                %> No accounts found!!! <%
            } else for (Account account : accounts) {  %>
            <table class="table" >
            <tr>
                <td>Number:</td>
                <td><%= account.number%></td>
             </tr>
            <tr>
                <td>Amount:</td>
                <td><%= account.money%></td>
            </tr>
            <tr>
                <td>Date opened:</td>
                <td><%= account.dateOpened%></td>
            </tr>
                <% if (account.dateClosed != null) %>
            <tr>
                <td>Date closed:</td>
                <td><%= account.dateClosed%></td>
            </tr>
                <%%>
            <hr width="400" align="left"/>
            </table>
            <a href="account_history.jsp?account_id=<%=account.number%>">Account history</a>
         <%   }
        %>
    </div>
</div>
</body>
</html>